<template>
  <div>
    <navigation-bar>
      <div class="searchContent">
        <custom-button
          type="custom"
          title="保存"
          @customClick="customClick"
        ></custom-button>
      </div>
    </navigation-bar>
    <div class="dataP">
      <div class="dataP-list">
        <div class="dataP-user">
          <table-no-height
            :dataSource="contentData"
            :tableColumns="contentTableColumns"
            :height="height"
            @rowClick="rowClick"
            :btnList="btnList"
            btnListWidth="80px"
            oprLable="只读"
            @switchChange="switchChange"
          ></table-no-height>
        </div>
        <div class="dataP-test">
          <el-tree
            ref="elTree"
            :data="treeTest"
            show-checkbox
            node-key="id"
            :props="defaultProps"
            :highlight-current="true"
          >
          </el-tree>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import customButton from "@/components/customComponents/customForm/customButton";
import tableNoHeight from "@/components/customComponents/customeTable/tableNoHeight";
import navigationBar from "@/components/customComponents/managementCenter/navigationBar";
import { AjaxCommon } from "@/libs/apiCommon";
import { PopupCommon } from "@/libs/popupCommon";
import { Utility } from "@/libs/utility";
export default {
  components: { tableNoHeight, navigationBar, customButton },
  data() {
    return {
      curUser: null,
      height: 100,
      contentData: [],
      contentTableColumns: [
        {
          prop: "realname",
          label: "真实姓名",
        },
        {
          prop: "username",
          label: "用户名",
        },
      ],
      btnList: ["switch"],
      listTest: [],
      treeTest: [],
      defaultProps: {
        children: "children",
        label: "label",
      },
    };
  },
  computed: {},
  mounted() {
    this.$nextTick(function () {
      this.getHeight();
    });
    /*窗口自适应，关键代码*/
    window.onresize = () => {
      this.getHeight();
    };
    this.getUserData();
    this.getTestData();
  },
  methods: {
    switchChange(d) {
      console.log(d);
    },
    customClick() {
      const data = this.$refs.elTree.getCheckedNodes();
      if (this.curUser == null || data.length < 1) {
        PopupCommon.notify("info", "请选择左侧用户且勾选右侧试验！");
        return;
      }
      const businessIdList = data
        .filter((t) => t.parentId != "ROOT")
        .map((t) => t.id);
      const params = {
        businessIdList: businessIdList, //试验id
        userCode: this.curUser.username, //用户代码=接口返回的username
        readonly: this.curUser.readonly,
        businessType: "exp", //业务类型 固定exp
      };
      AjaxCommon("/ccdc/cfg/dataAuthority/save", "post", params).then((res) => {
        if (res.data.code == 200) {
          PopupCommon.notify("success", "保存成功");
        } else {
          PopupCommon.notify("error", res.data.message);
        }
      });
    },
    getHeight() {
      this.height = window.innerHeight - 185;
    },
    rowClick(e) {
      this.curUser = e;
      this.clickGetTestData(e.username);
    },
    getUserData() {
      let self = this;
      AjaxCommon("/ccdc/cfg/dataAuthority/listUser", "get", {}).then((res) => {
        if (res.data.code == 200) {
          const data = res.data.result;
          data.map((t) => {
            if (typeof t.readonly != "undefined" && t.readonly == 0) {
              t.readonly = false;
            } else {
              t.readonly = true;
            }
          });
          self.contentData = data;
        } else {
          PopupCommon.notify("error", res.data.message);
        }
      });
    },
    getTestData() {
      let self = this;
      AjaxCommon("/ccdc/common/queryAllPmExpTree", "get", {}).then((res) => {
        if (res.data.code == 200) {
          self.listTest = res.data.result;
          const newData = JSON.parse(JSON.stringify(res.data.result));
          const treeData = Utility.arrayCascade(
            newData,
            (c, p) => c.parentId == p.id,
            "children"
          );
          self.treeTest = treeData;
        } else {
          PopupCommon.notify("error", res.data.message);
        }
      });
    },
    clickGetTestData(username) {
      let self = this;
      AjaxCommon(
        `/ccdc/cfg/dataAuthority/getByUserCodeAndType?userCode=${username}&businessType=exp`,
        "get",
        {}
      ).then((res) => {
        if (res.data.code == 200) {
          let checkKeys = [];
          if (res.data.result.businessIdList.length > 0) {
            checkKeys = res.data.result.businessIdList;
          }
          self.$refs.elTree.setCheckedKeys(checkKeys);
        } else {
          PopupCommon.notify("error", res.data.message);
        }
      });
    },
  },
};
</script>

<style scoped>
.dataP {
  margin: 10px auto;
}
.dataP-list {
  display: flex;
  height: calc(100vh - 170px);
}
.dataP-user {
  flex: 1;
  background-color: #fff;
}
.dataP-test {
  width: 500px;
  margin-left: 10px;
  background-color: #fff;
  height: calc(100vh - 170px);
  overflow-y: auto;
}
</style>
